package service.impl;

import java.util.List;

import javax.ejb.EJB;
import javax.ejb.Stateless;

import model.Station;
import service.StationService;
import dao.StationDAO;

@Stateless
public class StationServiceImpl implements StationService {

	@EJB
	private StationDAO stationDAO;

	@Override
	public List<Station> findAll() {
		return stationDAO.findAll();
	}

	@Override
	public Station findById(Integer id) {
		return stationDAO.find(id);
	}

	@Override
	public String save(Station station) {
		if (stationDAO.findByName(station.getStationName()) == null) {
			stationDAO.save(station);
			return "ok";
		}
		return "This station name already exists!";	 
	}


	@Override
	public void delete(Station station) {
		stationDAO.delete(station);			
	}

	@Override
	public String update(Station station) {
		Station temp = stationDAO.find(station.getStationID());
		if (temp != null) {
			Station name = stationDAO.findByName(station.getStationName());
			if (name == null) {
				if (temp.getStationName().equals(station.getStationName())) {
					return "ok";
				}
				temp.setStationName(station.getStationName());
				stationDAO.update(temp);
				return "ok";
			} else {
				if (name.getStationID() == temp.getStationID()) {
					temp.setStationName(station.getStationName());
					stationDAO.update(temp);
					return "ok";
				} else {
					return "This station name already exists!";	 
				}
			}
		} else {
			return "Station with this id wasn't found, update your list";
		}
	}
}
